package com.itheima.leetcode;

/**
 * 查找链表中间节点
 *
 * @FileName E08LeetCode876
 * @Description
 * @Author Sword
 * @date 2025-04-24
 **/
public class E08LeetCode876 {
    /*
                p1
                        p2
        1   2   3   4   5   null

                    p1
                                p2
        1   2   3   4   5   6   null
     */
    public static ListNode middleNode(ListNode head) {
        ListNode p1 = head;
        ListNode p2 = head;

        while (p2 != null && p2.next != null) {
            p1 = p1.next;
            p2 = p2.next.next;

        }
        return p1;

    }

    public static void main(String[] args) {
        ListNode head = ListNode.of(1, 2, 3, 4, 5);

        ListNode middleNode = middleNode(head);
        System.out.println(middleNode);
    }
}
